Newsgroups: comp.sys.sgi.apps
From: davea@quasar.mti.sgi.com (David B.Anderson)
Subject: emacs 19.22 unexelfsgi.c patch allows dbx use
Organization: Silicon Graphics, Inc.  Mountain View, CA
Date: Mon, 17 Jan 1994 21:49:43 GMT

The following is a context diff of a patch to 
unexelfsgi.c.  This allows use of dbx or WorkShop on the
unexec'd emacs image.    The problem was wierdness in the
mdebug section requiring addition of some sgi-specific code.

*** unexelfsgi.c	1994/01/16 21:54:40 old
--- unexelfsgi.c	1994/01/17 21:33:19 new
***************
*** 418,423 ****
--- 418,424 ----
  #include <unistd.h>
  #include <fcntl.h>
  #include <elf.h>
+ #include <syms.h> /* for HDRR declaration */
  #include <sys/mman.h>
  
  #ifndef emacs
***************
*** 493,498 ****
--- 494,500 ----
    Elf32_Addr new_data2_addr;
  
    int n, nn, old_bss_index, old_data_index, new_data2_index;
+   int old_mdebug_index;
    struct stat stat_buf;
  
    /* Open the old file & map it into the address space. */
***************
*** 523,528 ****
--- 525,545 ----
    old_section_names = (char *) old_base
      + OLD_SECTION_H(old_file_h->e_shstrndx).sh_offset;
  
+   /* Find the mdebug section, if any
+    */
+   for (old_mdebug_index = 1; old_mdebug_index < old_file_h->e_shnum; old_mdebug_index++)
+     {
+ #ifdef DEBUG
+       fprintf (stderr, "Looking for .mdebug - found %s\n",
+ 	       old_section_names + OLD_SECTION_H(old_mdebug_index).sh_name);
+ #endif
+       if (!strcmp (old_section_names + OLD_SECTION_H(old_mdebug_index).sh_name,
+ 		   ".mdebug"))
+ 	break;
+     }
+   if (old_mdebug_index == old_file_h->e_shnum)
+     old_mdebug_index = -1; /* just means no such section was present */
+ 
    /* Find the old .bss section.  Figure out parameters of the new
     * data2 and bss sections.
     */
***************
*** 748,753 ****
--- 765,819 ----
        
        memcpy (NEW_SECTION_H(nn).sh_offset + new_base, src,
  	      NEW_SECTION_H(nn).sh_size);
+ 
+       /* Adjust  the HDRR offsets in .mdebug and copy the 
+        * line data if it's in its usual 'hole' in the object.
+        * Makes the new file debuggable with dbx.
+        * patches up two problems: the absolute file offsets
+        * in the HDRR record of .mdebug (see /usr/include/syms.h), and
+        * the ld(1) bug that gets the line table in a hole in the
+        * elf file rather than in the .mdebug section proper.
+        * David Anderson. davea@sgi.com  Jan 16,1994
+        */
+       if(n == old_mdebug_index) {
+ #define MDEBUGADJUST(__ct,__fileaddr) \
+ if(n_phdrr->__ct > 0) { \
+ 	n_phdrr->__fileaddr += movement; \
+ }
+ 	HDRR * o_phdrr = (HDRR *)((byte *)old_base + OLD_SECTION_H(n).sh_offset);
+ 	HDRR * n_phdrr = (HDRR *)((byte *)new_base + NEW_SECTION_H(nn).sh_offset);
+ 	unsigned movement = new_data2_size;
+ 
+ 	MDEBUGADJUST(idnMax,cbDnOffset);
+ 	MDEBUGADJUST(ipdMax,cbPdOffset);
+ 	MDEBUGADJUST(isymMax,cbSymOffset);
+ 	MDEBUGADJUST(ioptMax,cbOptOffset);
+ 	MDEBUGADJUST(iauxMax,cbAuxOffset);
+ 	MDEBUGADJUST(issMax,cbSsOffset);
+ 	MDEBUGADJUST(issExtMax,cbSsExtOffset);
+ 	MDEBUGADJUST(ifdMax,cbFdOffset);
+ 	MDEBUGADJUST(crfd,cbRfdOffset);
+ 	MDEBUGADJUST(iextMax,cbExtOffset);
+ 	/* The Line Section, being possible off in a hole of the object,
+ 	 * requires special handling
+ 	 */
+ 	if(n_phdrr->cbLine > 0) {
+ 	  if(o_phdrr->cbLineOffset > (OLD_SECTION_H(n).sh_offset +
+ 			OLD_SECTION_H(n).sh_size)) {
+ 	    /* line data is in a hole in elf. do special copy and adjust
+                for this ld mistake.
+ 	     */
+ 	    n_phdrr->cbLineOffset += movement;
+ 
+ 	    memcpy(n_phdrr->cbLineOffset + new_base,
+ 	           o_phdrr->cbLineOffset + old_base, n_phdrr->cbLine);
+ 	  } else {
+ 	    /* somehow line data is in .mdebug as it is supposed to be!! ??
+ 	     */
+ 	    MDEBUGADJUST(cbLine,cbLineOffset);
+ 	  }
+ 	}
+       }
  
        /* If it is the symbol table, its st_shndx field needs to be patched. */
        if (NEW_SECTION_H(nn).sh_type == SHT_SYMTAB




This has received minimal testing, but does appear to work.
Your milage may vary.  No warrantees. 

Enjoy!
[ David B. Anderson             (415)390-4263             davea@sgi.com ]
[                      Visualize  Whirled Peas                          ]



